package com.damai.jobcenter;

import java.util.List;
import java.util.Set;

import org.quartz.SchedulerException;

import com.damai.action.factories.ApiJsonActionFactory;
import com.damai.annotation.Action;
import com.damai.annotation.Controller;
import com.damai.annotation.Crypt;
import com.damai.annotation.ReturnString;
import com.damai.annotation.Token;
import com.damai.error.ApiError;
import com.damai.error.ErrorUtil;

@Controller(key = "jobCenter")
@Action(ApiJsonActionFactory.class)
public class JobCenterApi {
	
	
	@Token
	@Crypt(Crypt.BOTH)
	public Boolean addJob(String jobName,String cron, String content) {
		try {
			QuartzPlugin.me().startJsJob(jobName, cron, content);
			return true;
		} catch (SchedulerException e) {
			throw new ApiError(ErrorUtil.printException(e));
		}
	}
	
	@Token
	@Crypt(Crypt.BOTH)
	public Boolean removeJob(String jobName) {
		try {
			QuartzPlugin.me().stopJob(jobName);
			return true;
		} catch (SchedulerException e) {
			throw new ApiError(ErrorUtil.printException(e));
		}
	}
	
	@Token
	@Crypt(Crypt.BOTH)
	@ReturnString
	public String type(String jobName){
		return QuartzPlugin.me().getType(jobName);
	}
	
	@Token
	@Crypt(Crypt.BOTH)
	public Set<String> jobNames(){
		return QuartzPlugin.me().getJobNames();
	}
	@Token
	@Crypt(Crypt.BOTH)
	public List<JobInfo> jobs(){
		return QuartzPlugin.me().getJobs();
	}
	
	
}
